package com.cxcy.swustlearner.controller;

import com.cxcy.swustlearner.pojo.User;
import com.cxcy.swustlearner.service.LoginService;
import com.cxcy.swustlearner.utils.JwtUtils;
import com.cxcy.swustlearner.utils.Md5;
import common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController()
public class LoginController {
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private LoginService loginService;
    @RequestMapping("/login")
    public Result login(String username, String password) {
        User user = loginService.login(username, Md5.md5(password));
        if (user != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("username", username);
            map.put("collegeId", user.getCollegeId());
            map.put("classId", user.getClassId());
            map.put("majorId", user.getMajorId());
            map.put("id", user.getId());
            map.put("permission", user.getPermission());
            map.put("studentId", user.getStudentId());
            map.put("uid", user.getUid());
            return Result.success(jwtUtils.generateJwt(map));
        }
            return Result.fail("用户名或密码错误");
    }
    /*
     * 退出登录需求由前端实现
     */
}